
-- how to read in a text file to an indefinite list of strings in ada

with Ada.Command_Line;      use Ada.Command_Line;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO;           use Ada.Text_IO;
with Ada.Containers.Vectors;
with Ada.Strings.Unbounded.Text_IO;
use Ada.Strings.Unbounded.Text_IO;

procedure ref_readfile is
    -- https://learn.adacore.com/courses/intro-to-ada/chapters/standard_library_containers.html
    package foo is new Ada.Containers.Vectors
        (index_type   => natural, element_type => unbounded_string);
    use foo;
    -- note: if a vector for another element type is to be defined e.g
   --package bar is new Ada.Containers.Vectors
       --(index_type   => natural, element_type => Integer);
   --use bar;
    -- then string vector 'v'  must be defined like so
    --v : foo.vector;
    v      : vector;

    function readlines(filename: in string) return vector is
        asdf   : file_type;
        buffer : unbounded_string;
        lines  : vector;
    begin 
        open(asdf, mode => in_file, name => filename);
        while not end_of_file(asdf) loop
            get_line(asdf, buffer);
            lines.append(buffer);
        end loop;
        close(asdf);
        return lines;
    end readlines;

    input  : string := argument(1);
begin
    if argument_count < 1 then
        put_line("requires one argument (name of text file)");
        return;
    end if;
    v := readlines(input);
    for s of v loop
        put_line(to_string(s));
    end loop;
end ref_readfile;
